import VueRouter from "vue-router";

import { updateTraffic } from "@/api/statistics";
import { v4 as uuidv4 } from "uuid";

import MyLayout from "@/layout/MyLayout.vue";
import MyHome from "@/layout/MyHome.vue";
import MyDoc from "@/layout/MyDoc.vue";

const router = new VueRouter({
  routes: [
    {
      path: "/",
      component: MyLayout,
      name: "/",
      children: [
        {
          path: "",
          component: MyHome,
          name: "home",
        },
        {
          path: "/doc",
          component: MyDoc,
          name: "doc",
        },
        {
          path: "/doc/:id",
          component: MyDoc,
          name: "doc",
        },
      ],
    },
  ],
});

// 全局前置守卫
// router.beforeEach((to, from, next) => {
//   console.log("to", to);
//   console.log("from", from);
//   let uniqueId = localStorage.getItem("uniqueId");
//   if (uniqueId == null) {
//     uniqueId = uuidv4();
//     localStorage.setItem("uniqueId", uniqueId);
//   }
//   updateTraffic({ pageUrl: to.name, uniqueId: uniqueId });
//   next();
// });

// 全局后置守卫
router.afterEach((to) => {
  console.log("to", to);
  let uniqueId = localStorage.getItem("uniqueId");
  if (uniqueId == null) {
    uniqueId = uuidv4();
    localStorage.setItem("uniqueId", uniqueId);
  }
  updateTraffic({ pageUrl: to.name, uniqueId: uniqueId });
});

export default router;
